{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导包\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "# 1、向量\n",
    "vec = np.array([1,2,3,4,5,6])\n",
    "print(vec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、矩阵\n",
    "mon = np.array([[1,2,3],[4,5,6]])\n",
    "mon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([['1','2','3'],['1','1','1']])\n",
    "\n",
    "# 类型转换\n",
    "arr = arr.astype(\"float\")\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出第一行\n",
    "mon[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出第一列\n",
    "mon[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过下标获取元素\n",
    "mon[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mon[-1,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],\n",
       "       [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],\n",
       "       [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],\n",
       "       [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],\n",
       "       [ 2,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array(\n",
    "    [\n",
    "        [1,2,3,4,5,6,7,8,9,10,11],\n",
    "        [1,2,3,4,5,6,7,8,9,10,11],\n",
    "        [1,2,3,4,5,6,7,8,9,10,11],\n",
    "        [1,2,3,4,5,6,7,8,9,10,11],\n",
    "        [2,2,3,4,5,6,7,8,9,10,11]\n",
    "    ]\n",
    ")\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  3,  5,  7,  9, 11],\n",
       "       [ 1,  3,  5,  7,  9, 11],\n",
       "       [ 1,  3,  5,  7,  9, 11],\n",
       "       [ 1,  3,  5,  7,  9, 11],\n",
       "       [ 1,  3,  5,  7,  9, 11]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:,::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  2],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1],\n",
       "       [11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[::-1,::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  1,  1,  1,  2],\n",
       "       [ 2,  2,  2,  2,  2],\n",
       "       [ 3,  3,  3,  3,  3],\n",
       "       [ 4,  4,  4,  4,  4],\n",
       "       [ 5,  5,  5,  5,  5],\n",
       "       [ 6,  6,  6,  6,  6],\n",
       "       [ 7,  7,  7,  7,  7],\n",
       "       [ 8,  8,  8,  8,  8],\n",
       "       [ 9,  9,  9,  9,  9],\n",
       "       [10, 10, 10, 10, 10],\n",
       "       [11, 11, 11, 11, 11]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转置\n",
    "arr.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 6],\n",
       "       [1, 4, 6],\n",
       "       [1, 4, 6],\n",
       "       [1, 4, 6],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:,[0,3,5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[(0,1,2,3,4),(0,1,2,3,4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  4,  5,  6,  7,  8,  9, 10, 11,  3,  4,  5,  6,  7,  8,  9, 10,\n",
       "       11,  3,  4,  5,  6,  7,  8,  9, 10, 11,  3,  4,  5,  6,  7,  8,  9,\n",
       "       10, 11,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 布尔值索引\n",
    "arr[arr > 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2, 2, 2, 2])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr == 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True, False, False, False, False,\n",
       "        False, False],\n",
       "       [ True,  True,  True,  True,  True, False, False, False, False,\n",
       "        False, False],\n",
       "       [ True,  True,  True,  True,  True, False, False, False, False,\n",
       "        False, False],\n",
       "       [ True,  True,  True,  True,  True, False, False, False, False,\n",
       "        False, False],\n",
       "       [ True,  True,  True,  True,  True, False, False, False, False,\n",
       "        False, False]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr <= 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = arr[arr <= 5]\n",
    "a[a >= 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、数学运算\n",
    "x = np.array([1,2,3,4,5,6,7,8])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 1.69314718, 2.09861229, 2.38629436, 2.60943791,\n",
       "       2.79175947, 2.94591015, 3.07944154])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.log(x) + 1 \n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2、对位运算\n",
    "arr1 = np.array([1,2,3,4,5,6,7,8])\n",
    "arr2 = np.array([1,2,3,4,5,6,7,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  6,  8, 10, 12, 14, 16])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  9, 16, 25, 36, 49, 64])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 * arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([       1,        4,       27,      256,     3125,    46656,\n",
       "         823543, 16777216], dtype=int32)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 ** arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 5],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、矩阵相乘\n",
    "arr1 = np.array(\n",
    "    [\n",
    "        [1,2],\n",
    "        [3,4],\n",
    "        [5,6]\n",
    "    ]\n",
    ")\n",
    "arr2 = arr1.T\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[35, 44],\n",
       "       [44, 56]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、矩阵相乘\n",
    "arr2.dot(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
